Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Jun 03, 2009 @ 12:45
    Ismail Mayat
    0

    root media folder issue [SOLVED]

    Guys,

    I have an action handler for document when its created a media folder is also created in the root of the media section. When document is updated if the media folder does not exist then it also creates the media folder in the root. My code to test if media folder already exists looks like this:

    [code]


    private int createMediaFolder(Document sender)
    {

    int id = 0;
    try
    {

    Media root = new Media(Config.Settings.MediaFolderRootID);
    id = getExistingFolderID(root, sender.Text);
    //media folder does not already exist
    if (id == 0)
    {

    Media m = Media.MakeNew(sender.Text, new MediaType(1031), UmbracoEnsuredPage.CurrentUser, root.Id);
    id = m.Id;
    }

    }
    catch (Exception ex)
    {
    //log
    errors = true;
    errorMessage += "\n\n" + ex.Message.ToString();
    Log.Add(LogTypes.Error, sender.Id, ex.Message.ToString());
    }
    return id;
    }


    private int getExistingFolderID(Media root, string folderName)
    {
    int folderID=0;

    foreach (Media m in root.Children) {
    if (m.Text == folderName) {

    folderID = m.Id;
    return folderID;
    }
    }

    return folderID;
    }

    [/code]


    Config.Settings.MediaFolderRootID that is value in config file and is set to -1 which corresponds to Media root folder. The issue is that when i do root.Children it comes back with "SYSTEM DATA: umbraco master root" ie the Media folder and not the children which exist in the root.

    Anyone else had this issue?

    Regards

    Ismail

  • Adam 152 posts 21 karma points
    Jun 03, 2009 @ 13:07
    Adam
    1

    Try,

    [code]
    private int getExistingFolderID(Media root, string folderName)
    {
    Media[] roots = Media.GetRootMedias();
    foreach (Media root in roots)
    {
    if(root.Text.Equals(MediaRootName))
    {
    return root.Id
    }
    }
    return 0;
    }
    [/code]

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Jun 03, 2009 @ 13:10
    Ismail Mayat
    0

    Adz,

    Nice one mate works a treat, as you said in your tweet

    [quote]
    each root folder is its own tree, and i dont think they share a common parent
    [/quote]

    Regards

    Ismail

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies